/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.showcase.client.activity;

import com.cleangwt.bootstrap.client.ui.Alert;
import com.cleangwt.bootstrap.client.ui.GroupFormPanel;
import com.cleangwt.bootstrap.client.ui.Modal;
import com.cleangwt.bootstrap.support.client.TooltipErrorHandler;
import com.cleangwt.json.client.SimpleObject;
import com.cleangwt.showcase.client.ProvidesSource;
import com.cleangwt.showcase.client.resource.i18n.UserManagerMessages;
import com.cleangwt.uibinder.client.RequiresActionRebind;
import com.cleangwt.uibinder.client.RequiresFormRebind;
import com.cleangwt.uibinder.client.UiHandler;
import com.cleangwt.uibinder.client.UiInput;
import com.cleangwt.user.client.activity.ForwardableActivity;
import com.cleangwt.user.client.ui.Button;
import com.cleangwt.user.client.ui.DateBox;
import com.cleangwt.user.client.ui.Lay;
import com.cleangwt.user.client.ui.NumberBox;
import com.cleangwt.user.client.ui.TextBox;
import com.cleangwt.user.client.ui.ValueCallback;
import com.cleangwt.validation.client.Validator;
import com.cleangwt.validation.client.constraints.NotNull;
import com.cleangwt.web.client.Requests;
import com.cleangwt.web.client.StatedCallback;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;

/**
 * 
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
public abstract class CwUserDetail extends ForwardableActivity implements ProvidesSource,
    RequiresActionRebind, RequiresFormRebind {
  @UiInput
  public TextBox address;
  @UiInput
  public NumberBox age;
  @UiInput
  public DateBox birth;

  @UiInput
  @NotNull
  public TextBox name;

  public Button btnCancel;
  public Button btnDelete;
  public Button btnReset;
  public Button btnSave;
  public UserManagerMessages c;
  public SimpleObject record;
  public Validator validator;

  public CwUserDetail() {
    c = GWT.create(UserManagerMessages.class);
    name = new TextBox();
    age = new NumberBox();
    address = new TextBox();
    birth = new DateBox();
    btnReset = new Button(c.reset());
    btnCancel = new Button(c.cancel());
    btnDelete = new Button(c.delete());
    btnSave = new Button(c.save());
    validator = new Validator(new TooltipErrorHandler());
    GroupFormPanel form = new GroupFormPanel();
    form.buildVertical().add(c.name(), name).div(3).add(c.age(), age).div(3).add(c.address(),
        address).div(3).add(c.birth(), birth).div(3);
    form.add(Lay.pullRight(Lay.p(btnReset, btnCancel, btnDelete, btnSave)));
    this.addConstraints(validator);
    addEventHandlers();
    validator.activate();
    // return form;
    initWidget(form);
  }

  @UiHandler("btnCancel")
  void onBtnCancelClick(ClickEvent event) {
    // manager.setActive(0);
    forward(0);
  }

  @UiHandler("btnDelete")
  void onBtnDeleteClick(ClickEvent event) {
    Modal.confirm(c.confirm(), new ValueCallback<String>() {

      @Override
      public void onValueCallback(String option) {
        Requests.postObject("user/delete.do", record, new StatedCallback() {

          @Override
          protected void onSuccess(String result) {
            forward(0);
            Alert.alert(c.deleteSucceed());
          }
        });

      }
    });
  }

  @UiHandler("btnReset")
  void onBtnResetClick(ClickEvent event) {
    this.setFormData(record);
    validator.reset();
  }

  @UiHandler(value = "btnSave", validate = "validator")
  void onBtnSaveClick(ClickEvent event) {
    record = this.getFormData(record);
    Requests.postObject("user/save.do", record, new StatedCallback() {

      @Override
      protected void onSuccess(String result) {
        Alert.alert(c.saveSucceed());
        forward(0);
      }

    });
  }

  public void setRecord(SimpleObject record) {
    this.record = record;
    validator.reset();

    this.setFormData(record);
    btnDelete.setEnabled(record != null);
  }

}
